package com.ruoyi.agent.controller;

import java.util.List;

import com.ruoyi.agent.domain.Agent;
import com.ruoyi.agent.service.IAgentService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门店代理人Controller
 *
 * @author ruoyi
 * @date 2022-05-24
 */
@RestController
@RequestMapping("/system/agent")
public class AgentController extends BaseController
{
    @Autowired
    private IAgentService agentService;

    /**
     * 查询门店代理人列表
     */
    @PreAuthorize("@ss.hasPermi('system:agent:list')")
    @GetMapping("/list")
    public TableDataInfo list(Agent agent)
    {
        startPage();
        List<Agent> list = agentService.selectAgentList(agent);
        return getDataTable(list);
    }

    /**
     * 获取门店代理人详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:agent:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(agentService.selectAgentById(id));
    }

    /**
     * 新增门店代理人
     */
    @PreAuthorize("@ss.hasPermi('system:agent:add')")
    @Log(title = "门店代理人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Agent agent)
    {
        Agent query = new Agent();
        query.setAgentPhone(agent.getAgentPhone());
        List<Agent> list = agentService.selectAgentList(query);
        if(null != list && list.size() > 0){
            return AjaxResult.error("手机号已经存在");
        }
        //赋值邀请码
        agent.setAgentCode(createAgentCode());
        agent.setCreateBy(getUserId());
        return toAjax(agentService.insertAgent(agent));
    }

    /**
     * 生成邀请码
     * @return
     */
    private String createAgentCode(){
        String agent_code = (int)((Math.random()*9+1)*100000)+"";
        Agent query = new Agent();
        query.setAgentCode(agent_code);
        List<Agent> list = agentService.selectAgentList(query);
        if(null == list || list.size() == 0){
            return agent_code;
        }else{
            return createAgentCode();
        }
    }

    /**
     * 修改门店代理人
     */
    @PreAuthorize("@ss.hasPermi('system:agent:edit')")
    @Log(title = "门店代理人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Agent agent)
    {
        Agent query = new Agent();
        query.setAgentPhone(agent.getAgentPhone());
        query.getParams().put("id",agent.getId());
        List<Agent> list = agentService.selectAgentList(query);
        if(null != list && list.size() > 0){
            return AjaxResult.error("手机号已经存在");
        }
        agent.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(agentService.updateAgent(agent));
    }

    /**
     * 删除门店代理人
     */
    @PreAuthorize("@ss.hasPermi('system:agent:remove')")
    @Log(title = "门店代理人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(agentService.logicDeleteAgent(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
